package com.shudes.pt.builder;

import java.util.*;

import org.hibernate.*;
import org.hibernate.criterion.*;

import com.shudes.pt.db.*;
import com.shudes.pt.pojo.*;
import com.shudes.util.*;

public class PTSessionBuilder {

	SessionContext sessionContext;
	
	public PTSessionBuilder(SessionContext context) {
		sessionContext=context;		
	}
	
	@SuppressWarnings("unchecked")
	public PTSession buildById(Long id) { 
		Session session; 
		PTSession ptSess=null;
		List<PTSession> sessions;
		
		session = sessionContext.getSession();
		session.beginTransaction();
		
		sessions = session.createCriteria(PTSession.class).add(Restrictions.idEq(id)).list();
		if(sessions.size()>0) 
			ptSess = (PTSession) sessions.get(0); 
		
		return ptSess;		
	}
	
	
	@SuppressWarnings("unchecked")
	public List<PTSession> buildForDate(Player player, Date on) {
		Session session;
		List ptSessions;
		Date start;
		Date end;
		
		start = Dates.truncateDate(on); 
		end = Dates.endOfDay(on);
		
//		System.out.println("start-> " + Dumper.INSTANCE.dump(start));
		
		session = sessionContext.getSession();
		session.beginTransaction();
		ptSessions = 
			session.createCriteria(PTSession.class)
			.add(Restrictions.eq("player",player))//.list();
			
			.add(Restrictions.gt("start",start))
			.add(Restrictions.lt("start", end))
			.list();
		
		session.getTransaction().commit();
//		System.out.println(ptSessions.size());
		return ptSessions;
	}

}
